import psutil
import time
from datetime import datetime

from localServer import app


def is_process_running(process_name):
    """检查进程是否正在运行"""
    for proc in psutil.process_iter(['name']):
        if process_name.lower() in proc.info['name'].lower():
            return True
    return False


if __name__ == "__main__":
    process_name = "localServer.exe"

    while True:
        current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")  # 获取当前系统时间

        if not is_process_running(process_name):
            print(f"{current_time} - {process_name} is not running. Starting the process...")
            app.run(debug=False, port=12345)

        else:
            print(f"{current_time} - {process_name} is already running.")
        # 等待3秒钟再次检查
        time.sleep(3);
